# -*- coding: utf-8 -*-
#
# The Philatelist program is a fork of the OpenNumismat program
# Copyright (c) 2011 - 2024 Vitaly Ignatov (OpenNumismat) <opennumismat@gmail.com>
# Copyright (c) 2021 - 2024 Victor Nabatov (Philatelist) <nabatov.victor@gmail.com>
#

from PySide6.QtCore import Qt, QMargins, QT_TRANSLATE_NOOP
from PySide6.QtCore import Signal as pyqtSignal
from PySide6.QtGui import QIcon, QAction, QPixmap
from PySide6.QtWidgets import (
    QApplication, QCheckBox, QColorDialog, QComboBox, QDialog, QDialogButtonBox, QDoubleSpinBox,
    QFileDialog, QFormLayout, QGroupBox, QHBoxLayout, QLabel, QLineEdit, QListWidget,
    QListWidgetItem, QMenu, QMessageBox, QPushButton, QSizePolicy, QSpinBox, QStyle,
    QStyleFactory, QTabWidget, QTreeWidget, QTreeWidgetItem, QVBoxLayout, QWidget
)

from Philatelist.StampEditor.FormItems import NumberEdit
from Philatelist.Collection.CollectionFields import CollectionFieldsBase, CollectionField, ImagesFields
from Philatelist.Reports import Report
from Philatelist.Tools.DialogDecorators import storeDlgSizeDecorator
from Philatelist.Tools.Gui import statusIcon
from Philatelist.Tools.Gui import infoMessageBox
from Philatelist.Settings import Settings
from Philatelist.Collection.CollectionFields import Statuses, TitleTemplateFields
from Philatelist.Collection.Import.Cache import Cache


class MainSettingsPage(QWidget):

    Languages = (
#        ("Български", 'bg'), ("Català", 'ca'), ("Čeština", 'cs'), ("Deutsch", 'de'),
#        ("English", 'en'), ("Ελληνικά", 'el'), ("Español", 'es'), ("فارسی", 'fa'), ("Français", 'fr'),
#        ("Italiano", 'it'), ("Latviešu", 'lv'), ("Magyar", 'hu'), ("Nederlands", 'nl'),
#        ("Polski", 'pl'), ("Português", 'pt'), ("Русский", 'ru'), ("Svenska", 'sv'),
#        ("Türkçe", 'tr'), ("Український", 'uk')
        ("English", 'en'), ("Русский", 'ru')
    )

    def __init__(self, collection, parent=None):
        super().__init__(parent)
        settings = Settings()
        style = QApplication.style()
        layout = QFormLayout()
        layout.setRowWrapPolicy(QFormLayout.WrapLongRows)
        self.languageSelector = QComboBox(self)
        for lang in self.Languages:
            self.languageSelector.addItem(lang[0], lang[1])

        current = self.languageSelector.findData(settings['locale'])
        if current == -1:
            current = 0

        self.languageSelector.setCurrentIndex(current)
        self.languageSelector.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        layout.addRow(self.tr("Language"), self.languageSelector)

        self.reference = QLineEdit(self)
        self.reference.setMinimumWidth(120)
        self.reference.setText(settings['reference'])
        icon = style.standardIcon(QStyle.SP_DialogOpenButton)
        self.referenceButton = QPushButton(icon, '', self)
        self.referenceButton.clicked.connect(self.referenceButtonClicked)
        if collection.isReferenceAttached():
            self.reference.setDisabled(True)
            self.referenceButton.setDisabled(True)

        hLayout = QHBoxLayout()
        hLayout.addWidget(self.reference)
        hLayout.addWidget(self.referenceButton)
        hLayout.setContentsMargins(QMargins())
        layout.addRow(self.tr("Reference"), hLayout)

        self.backupFolder = QLineEdit(self)
        self.backupFolder.setMinimumWidth(120)
        self.backupFolder.setText(settings['backup'])
        icon = style.standardIcon(QStyle.SP_DirOpenIcon)
        self.backupFolderButton = QPushButton(icon, '', self)
        self.backupFolderButton.clicked.connect(self.backupButtonClicked)

        hLayout = QHBoxLayout()
        hLayout.addWidget(self.backupFolder)
        hLayout.addWidget(self.backupFolderButton)
        hLayout.setContentsMargins(QMargins())
        layout.addRow(self.tr("Backup folder"), hLayout)

        self.autobackup = QCheckBox(self.tr("Make autobackup"), self)
        self.autobackup.setChecked(settings['autobackup'])
        self.autobackup.stateChanged.connect(self.autobackupClicked)
        layout.addRow(self.autobackup)

        self.autobackupDepth = QSpinBox(self)
        self.autobackupDepth.setRange(1, 1000)
        self.autobackupDepth.setValue(settings['autobackup_depth'])
        self.autobackupDepth.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        layout.addRow(self.tr("Stamp changes before autobackup"), self.autobackupDepth)
        self.autobackupDepth.setEnabled(settings['autobackup'])

        self.speedup = QComboBox(self)
        self.speedup.addItem(self.tr("Reliable"), 0)
        self.speedup.addItem(self.tr("Fast"), 1)
        self.speedup.setCurrentIndex(settings['speedup'])
        self.speedup.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        layout.addRow(self.tr("Acceleration of storage"), self.speedup)

        self.templateSelector = QComboBox(self)
        for template in Report.scanTemplates():
            self.templateSelector.addItem(template[0], template[1])

        current = self.templateSelector.findData(settings['template'])
        if current == -1:
            current = 0

        self.templateSelector.setCurrentIndex(current)
        self.templateSelector.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        layout.addRow(self.tr("Default template"), self.templateSelector)

        self.imagesByDefault = QSpinBox(self)
        self.imagesByDefault.setRange(1, len(ImagesFields))
        self.imagesByDefault.setValue(settings['images_by_default'])
        self.imagesByDefault.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        layout.addRow(self.tr("Images count by default"), self.imagesByDefault)

        self.builtInEditor = QCheckBox(self.tr("Use built-in image editor"), self)
        self.builtInEditor.setChecked(settings['built_in_editor'])
        layout.addRow(self.builtInEditor)

        self.styleSelector = QComboBox(self)
        for key in QStyleFactory.keys():
            self.styleSelector.addItem(key)

        self.styleSelector.setCurrentText(settings['style'])
        self.styleSelector.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        layout.addRow(self.tr("Style"), self.styleSelector)

        self.transparentColorButton = QPushButton(self)
        self.transparentColorButton.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.transparent_color = settings['transparent_color']
        self.updateTransparentColorButton(self.transparent_color)
        layout.addRow(self.tr("Image background color"), self.transparentColorButton)
        self.transparentColorButton.clicked.connect(self.transparentColorButtonClicked)
        self.setLayout(layout)

    def backupButtonClicked(self):
        folder = QFileDialog.getExistingDirectory(
            self, self.tr("Backup folder"), self.backupFolder.text())

        if folder:
            self.backupFolder.setText(folder)

    def autobackupClicked(self, state):
        self.autobackupDepth.setEnabled(state == Qt.Checked)

    def referenceButtonClicked(self):
        file, _selectedFilter = QFileDialog.getOpenFileName(
            self, self.tr("Select reference"), self.reference.text(), "*.ref")

        if file:
            self.reference.setText(file)

    def transparentColorButtonClicked(self):
        settings = Settings()
        color = settings['transparent_color']
        dlg = QColorDialog(color, self)
        if dlg.exec_() == QDialog.Accepted:
            self.transparent_color = dlg.currentColor()
            settings['transparent_color'] = self.transparent_color
            self.updateTransparentColorButton(self.transparent_color)

    def updateTransparentColorButton(self, color):
        pixmap = QPixmap(16, 16)
        pixmap.fill(color)
        icon = QIcon(pixmap)
        self.transparentColorButton.setIcon(icon)

    def save(self):
        settings = Settings()
        settings['locale'] = self.languageSelector.currentData()
        settings['backup'] = self.backupFolder.text()
        settings['autobackup'] = self.autobackup.isChecked()
        settings['autobackup_depth'] = self.autobackupDepth.value()
        settings['reference'] = self.reference.text()
        settings['speedup'] = self.speedup.currentData()
        settings['template'] = self.templateSelector.currentData()
        settings['images_by_default'] = self.imagesByDefault.value()
        settings['built_in_editor'] = self.builtInEditor.isChecked()
        settings['style'] = self.styleSelector.currentText()
        settings['transparent_color'] = self.transparent_color
        settings.save()


class FieldAction(QAction):

    clicked = pyqtSignal(object)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.triggered.connect(self.trigger)

    def trigger(self):
        self.clicked.emit(self)


class CollectionSettingsPage(QWidget):

    def __init__(self, collection, parent=None):
        super().__init__(parent)
        self.collection = collection
        self.settings = collection.settings
        self.model = collection.model()
        style = QApplication.style()

        layout = QFormLayout()
        layout.setRowWrapPolicy(QFormLayout.WrapLongRows)
        self.titleTemplate = QLineEdit(self)
        self.titleTemplate.setText(str(self.settings['title_template']))
        self.addFieldButton = QPushButton(QIcon(':/add.png'), '', self)
        self.addFieldButton.setToolTip(self.tr("Add field"))

        menu = QMenu(self)
        menu.aboutToShow.connect(self.addFieldButtonClicked)
        self.addFieldButton.setMenu(menu)

        hLayout = QHBoxLayout()
        hLayout.addWidget(self.titleTemplate)
        hLayout.addWidget(self.addFieldButton)
        hLayout.setContentsMargins(QMargins())
        layout.addRow(self.tr("Title template"), hLayout)

        self.storeFolder = QLineEdit(self)
        self.storeFolder.setMinimumWidth(120)
        self.storeFolder.setText(str(self.settings['store']))
        icon = style.standardIcon(QStyle.SP_DialogOpenButton)
        self.storeButton = QPushButton(icon, '', self)
        self.storeButton.clicked.connect(self.storeButtonClicked)

        hLayout = QHBoxLayout()
        hLayout.addWidget(self.storeFolder)
        hLayout.addWidget(self.storeButton)
        hLayout.setContentsMargins(QMargins())
        layout.addRow(self.tr("Collection store"), hLayout)

        self.imageSideLen = NumberEdit(self)
        self.imageSideLen.setMaximumWidth(60)
        layout.addRow(self.tr("Max image side len"), self.imageSideLen)
        self.imageSideLen.setText(str(self.settings['ImageSideLen']))
        self.imageSideLen.setToolTip(self.tr("0 for storing in original size"))

        self.imageHeight = QDoubleSpinBox(self)
        self.imageHeight.setRange(1, 3)
        self.imageHeight.setDecimals(1)
        self.imageHeight.setSingleStep(0.1)
        self.imageHeight.setValue(self.settings['image_height'])
        self.imageHeight.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        layout.addRow(self.tr("Preview image height"), self.imageHeight)

        self.imagesAtBottom = QCheckBox(self.tr("Images at bottom"), self)
        self.imagesAtBottom.setChecked(self.settings['images_at_bottom'])
        layout.addRow(self.imagesAtBottom)

        self.freeNumeric = QCheckBox(self.tr("Free format numeric fields"), self)
        self.freeNumeric.setChecked(self.settings['free_numeric'])
        layout.addRow(self.freeNumeric)

        self.convertFraction = QCheckBox(
            self.tr("Convert 0.5 to ½ (support ¼, ½, ¾, 12¼, 13½...)"),
            self
        )
        self.convertFraction.setChecked(self.settings['convert_fraction'])
        layout.addRow(self.convertFraction)

        self.richText = QCheckBox(self.tr("Use RichText format"), self)
        self.richText.setChecked(self.settings['rich_text'])
        layout.addRow(self.richText)

        self.relativeUrl = QCheckBox(self.tr("Use relative paths for invoices"), self)
        self.relativeUrl.setChecked(self.settings['relative_url'])
        layout.addRow(self.relativeUrl)

        self.starsCount = QCheckBox(self.tr("5 stars in rating"), self)
        self.starsCount.setChecked(self.settings['stars_count'] == 5)
        layout.addRow(self.starsCount)

        self.tagsEnabled = QCheckBox(self.tr("Enable tags"), self)
        self.tagsEnabled.setChecked(self.settings['tags_used'])
        layout.addRow(self.tagsEnabled)

        self.statusUsed = {}
        statusesList = QListWidget(self)
        statusesList.setWrapping(True)
        for status in Statuses:
            title = self.settings[status + '_status_title']
            item = QListWidgetItem(statusIcon(status), title)
            item.setFlags(Qt.ItemIsEditable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsSelectable)
            checked = Qt.Unchecked
            if self.settings[status + '_status_used']:
                checked = Qt.Checked

            item.setCheckState(checked)
            statusesList.addItem(item)
            self.statusUsed[status] = item

        layout.addRow(self.tr("Used statuses (double-click for rename):"), QWidget())
        layout.addRow(statusesList)

        self.defaultStatus = QComboBox(self)
        for status in Statuses:
            self.defaultStatus.addItem(statusIcon(status), Statuses[status], status)

        index = self.defaultStatus.findData(self.settings['default_status'])
        self.defaultStatus.setCurrentIndex(index)
        self.defaultStatus.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        layout.addRow(self.tr("Default status for new stamp"), self.defaultStatus)
        self.setLayout(layout)

    def addFieldButtonClicked(self):
        self.addFieldButton.menu().clear()
        for field in self.collection.fields:
            if field.name in TitleTemplateFields:
                if field.enabled:
                    act = FieldAction(field.title, self)
                    act.setData(field.name)
                    self.addFieldButton.menu().addAction(act)
                    act.clicked.connect(self.addField)

    def addField(self, act):
        field = act.data()
        pos = self.titleTemplate.cursorPosition()
        template = self.titleTemplate.text()
        template = template[:pos] + '<' + field + '>' + template[pos:]
        self.titleTemplate.setText(template)

    def storeButtonClicked(self):
        folder = QFileDialog.getExistingDirectory(
            self,
            self.tr("Collection folder"),
            self.storeFolder.text()
        )
        if folder:
            self.storeFolder.setText(folder)

    def save(self):
        self.settings['title_template'] = self.titleTemplate.text()
        self.settings['free_numeric'] = self.freeNumeric.isChecked()
        self.settings['convert_fraction'] = self.convertFraction.isChecked()
        self.settings['ImageSideLen'] = int(self.imageSideLen.text())
        old_image_height = self.settings['image_height']
        self.settings['image_height'] = self.imageHeight.value()
        self.settings['images_at_bottom'] = self.imagesAtBottom.isChecked()
        self.settings['rich_text'] = self.richText.isChecked()
        self.settings['relative_url'] = self.relativeUrl.isChecked()
        self.settings['stars_count'] = 5 if self.starsCount.isChecked() else 10
        self.settings['tags_used'] = self.tagsEnabled.isChecked()
        self.settings['store'] = self.storeFolder.text()
        default_status = self.defaultStatus.currentData()
        self.settings['default_status'] = default_status
        for status in Statuses:
            title = self.statusUsed[status].text()
            self.settings[status + '_status_title'] = title
            is_checked = (self.statusUsed[status].checkState() == Qt.Checked)
            self.settings[status + '_status_used'] = is_checked

        # Default status is always enabled
        self.settings[default_status + '_status_used'] = True
        self.settings.save()

        if self.settings['image_height'] != old_image_height:
            result = QMessageBox.question(
                self,
                self.tr("Settings"),
                self.tr("Preview image height was changed. Recalculate it now?"),
                QMessageBox.Yes | QMessageBox.No,
                QMessageBox.Yes
            )
            if result == QMessageBox.Yes:
                self.model.recalculateAllImages(self)


class FieldsSettingsPage(QWidget):

    DataRole = Qt.UserRole

    def __init__(self, collection, parent=None):
        super().__init__(parent)
        self.treeWidget = QTreeWidget(self)
        self.treeWidget.setHeaderHidden(True)
        self.treeWidget.itemSelectionChanged.connect(self.itemSelectionChanged)
        self.settings = collection.settings

        self.groupItems = []
        stamp_item = self.groupItem(self.treeWidget, 'stamp_group_title')
        stamp_main_item = self.groupItem(stamp_item, 'stamp_main_group_title')
        stamp_state_item = self.groupItem(stamp_item, 'stamp_state_group_title')
        stamp_issue_item = self.groupItem(stamp_item, 'stamp_issue_group_title')
        stamp_parameters_item = self.groupItem(stamp_item, 'stamp_parameters_group_title')
        stamp_catalogue_item = self.groupItem(stamp_item, 'stamp_catalogue_group_title')
        stamp_design_item = self.groupItem(stamp_item, 'stamp_design_group_title')
        stamp_traits_item = self.groupItem(stamp_item, 'stamp_traits_group_title')

        market_item = self.groupItem(self.treeWidget, 'market_group_title')
        market_prices_item = self.groupItem(market_item, 'market_prices_group_title')
        market_buy_item = self.groupItem(market_item, 'market_buy_group_title')
        market_sale_item = self.groupItem(market_item, 'market_sale_group_title')

        images_item = self.groupItem(self.treeWidget, 'images_group_title')

        other_item = self.groupItem(self.treeWidget, 'other_group_title')
        system_item = self.groupItem(self.treeWidget, 'system_group_title')

        self.fields = collection.fields
        for field in self.fields:
            item = QTreeWidgetItem((field.title,))
            item.setData(0, self.DataRole, field)
            item.setFlags(
                Qt.ItemIsEditable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled | Qt.ItemIsSelectable
            )
            checked = Qt.Unchecked
            if field.enabled:
                checked = Qt.Checked

            item.setCheckState(0, checked)
            if field.name in ('id', 'sort_id', 'create_date', 'update_date', 'image'):
                system_item.addChild(item)

            elif field.name in (
                'photo1', 'photo1_title', 'photo2', 'photo2_title',
                'photo3', 'photo3_title', 'photo4', 'photo4_title'):
                images_item.addChild(item)

            elif field.name in (
                'region', 'country', 'title', 'denomination', 'year', 'stamp_year', 'theme', 'series', 'type', 'url'):
                stamp_main_item.addChild(item)

            elif field.name in ('catalognum1', 'catalognum2', 'catalognum3', 'catalognum4'):
                stamp_catalogue_item.addChild(item)

            elif field.name in (
                'status', 'format', 'cancellation', 'condition', 'quantity',
                'grade', 'certificate', 'expertise_date', 'expert', 'rating'):
                stamp_state_item.addChild(item)

            elif field.name in ('issue_date', 'emission_period', 'withdrawal', 'circulation'):
                stamp_issue_item.addChild(item)

            elif field.name in (
                'material', 'material_type', 'thickness', 'printing',
                'perforation', 'perforation_size', 'full_size', 'picture_size'):
                stamp_parameters_item.addChild(item)

            elif field.name in (
                'stamp_image', 'reverse_image', 'stamp_design', 'stamp_reverse', 'designer', 'engraver',
                'stamp_color1', 'stamp_color2', 'glue', 'glue_color', 'variety', 'variety_desc', 'variety_image',
                'grid_image', 'honeycomb_image', 'watermark_image', 'raster_image'):
                stamp_design_item.addChild(item)

            elif field.name in ('grid', 'honeycomb', 'watermark', 'raster', 'features'):
                stamp_traits_item.addChild(item)

            elif field.name in ('price1', 'price2', 'price3', 'price4', 'price5', 'price6', 'currency'):
                market_prices_item.addChild(item)

            elif field.name in (
                'pay_date', 'pay_price', 'total_pay_price', 'seller', 'pay_place', 'pay_info', 'buying_invoice'):
                market_buy_item.addChild(item)

            elif field.name in (
                'sale_date', 'sale_price', 'total_sale_price', 'buyer', 'sale_place', 'sale_info', 'sale_invoice'):
                market_sale_item.addChild(item)
            else:
                other_item.addChild(item)

        self.treeWidget.expandAll()

        self.renameButton = QPushButton(self.tr("Rename"), self)
        self.renameButton.clicked.connect(self.renameButtonClicked)
        self.renameButton.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.renameButton.setEnabled(False)

        self.defaultFieldsButton = QPushButton(self.tr("Revert to default"), self)
        self.defaultFieldsButton.clicked.connect(self.defaultFieldsButtonClicked)
        self.defaultFieldsButton.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

        layout = QVBoxLayout()
        layout.addWidget(QLabel(self.tr("Global enabled fields:"), self))
        layout.addWidget(self.treeWidget)

        hLayout = QHBoxLayout()
        hLayout.addWidget(self.renameButton, alignment=Qt.AlignLeft)
        hLayout.addWidget(self.defaultFieldsButton, alignment=Qt.AlignRight)
        layout.addLayout(hLayout)
        self.setLayout(layout)

    def groupItem(self, parent, group_name):
        item = QTreeWidgetItem(parent, (self.settings[group_name],))
        item.setData(0, self.DataRole, group_name)
        item.setFlags(Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable)
        self.groupItems.append(item)
        return item

    def itemSelectionChanged(self):
        self.renameButton.setEnabled(len(self.treeWidget.selectedItems()) > 0)

    def defaultFieldsButtonClicked(self):
        defaultFields = CollectionFieldsBase()
        for item in self.treeChilds(self.treeWidget, True):
            field = item.data(0, self.DataRole)
            if isinstance(field, CollectionField):
                defaultField = defaultFields.field(field.id)
                item.setText(0, defaultField.title)

            elif isinstance(field, str):
                item.setText(0, self.settings.Default[field])

        for item in self.groupItems:
            group_name = item.data(0, self.DataRole)
            group_title = self.settings.Default[group_name]
            item.setText(0, group_title)

    def renameButtonClicked(self):
        items = self.treeWidget.selectedItems()
        if len(items) > 0:
            self.treeWidget.editItem(items[0])

    def save(self):
        for item in self.treeChilds(self.treeWidget, True):
            field = item.data(0, self.DataRole)
            if isinstance(field, CollectionField):
                field.enabled = (item.checkState(0) == Qt.Checked)
                field.title = item.text(0)

            elif isinstance(field, str):
                self.settings[field] = item.text(0)

        self.fields.save()
        self.settings.save()

    def treeChilds(self, tree, with_parent=False):
        childs = []
        for i in range(tree.topLevelItemCount()):
            top_item = tree.topLevelItem(i)
            if with_parent:
                childs.append(top_item)

            childs.extend(self.treeItemChilds(top_item, with_parent))

        return childs

    def treeItemChilds(self, tree_item, with_parent=False):
        childs = []
        for j in range(tree_item.childCount()):
            item = tree_item.child(j)
            if item.childCount():
                if with_parent:
                    childs.append(item)

                childs.extend(self.treeItemChilds(item))
            else:
                childs.append(item)

        return childs


class ImportSettingsPage(QWidget):
    Languages = (
        ('ar', 'العربية'), ('az', 'Azərbaycanca'), ('be', 'Беларуская'),
        ('bg', 'Български'), ('ca', 'Català'), ('cs', 'Česky'),
        ('da', 'Dansk'), ('de', 'Deutsch'), ('el', 'Ελληνικά'),
        ('en', 'English'), ('es', 'Español'), ('et', 'Eesti'), ('fa', 'فارسی'),
        ('fr', 'Français'), ('ko', '한국어'), ('hr', 'Hrvatski'),
        ('id', 'Bahasa Indonesia'), ('it', 'Italiano'), ('he', 'עברית'),
        ('ka', 'ქართული '), ('lv', 'Latviešu'), ('lt', 'Lietuvių'),
        ('hu', 'Magyar'), ('nl', 'Nederlands'), ('ja', '日本語'),
        ('no', 'Norsk'), ('pl', 'Polski'), ('pt', 'Português'),
        ('br', 'Português BR'), ('ro', 'Română'), ('ru', 'Русский'),
        ('sk', 'Slovenčina'), ('sl', 'Slovenščina'), ('sr', 'Српски'),
        ('fi', 'Suomi'), ('sv', 'Svenska'), ('th', 'ภาษาไทย '),
        ('tr', 'Türkçe'), ('uk', 'Українська'), ('vi', 'Tiếng Việt'),
        ('zh', '中文（简体）'), ('zt', '中文 (繁體)'),
        # ('ky', 'Кыргызча'), ('ta', 'தமிழ்'), ('hy', 'Հայերեն'),
        # ('sw', 'Kiswahili'), ('bn', 'বাংলা'), ('si', 'සිංහල'), ('gu', 'ગુજરાતી'),
        # ('ht', 'Kreyòl ayisyen'), ('mn', 'Монгол'), ('sq', 'Shqip'),
        # ('af', 'Afrikaans'), ('te', 'తెలుగు'), ('ml', 'മലയാളം'), ('hi', 'हिन्दी'),
        # ('tl', 'Filipino'), ('ms', 'Melayu'), ('pa', 'پنجابی'),
        # ('kk', 'Қазақша'), ('ur', 'اردو'), ('mk', 'Македонски'),
        # ('fy', 'Frysk'),
    )
    Currencies = (
        ('BGN', QT_TRANSLATE_NOOP("Currency", "BGN - Bulgarian lev")),
        ('BRL', QT_TRANSLATE_NOOP("Currency", "BRL - Brazilian real")),
        ('BYN', QT_TRANSLATE_NOOP("Currency", "BYN - Belarusian ruble")),
        ('CZK', QT_TRANSLATE_NOOP("Currency", "CZK - Czech koruna")),
        ('EUR', QT_TRANSLATE_NOOP("Currency", "EUR - Euro")),
        ('GBP', QT_TRANSLATE_NOOP("Currency", "GBP - Pound sterling")),
        ('HUF', QT_TRANSLATE_NOOP("Currency", "HUF - Hungarian forint")),
        ('PLN', QT_TRANSLATE_NOOP("Currency", "PLN - Polish złoty")),
        ('RUB', QT_TRANSLATE_NOOP("Currency", "RUB - Russian ruble")),
        ('SEK', QT_TRANSLATE_NOOP("Currency", "SEK - Swedish krona")),
        ('TRY', QT_TRANSLATE_NOOP("Currency", "TRY - Turkish lira")),
        ('UAH', QT_TRANSLATE_NOOP("Currency", "UAH - Ukrainian hryvnia")),
        ('USD', QT_TRANSLATE_NOOP("Currency", "USD - United States dollar"))
    )

    def __init__(self, parent=None):
        super().__init__(parent)
        settings = Settings()
        fLayout = QFormLayout()
        fLayout.setRowWrapPolicy(QFormLayout.WrapLongRows)
        self.colnect_locale = QComboBox(self)
        for lang in self.Languages:
            self.colnect_locale.addItem(lang[1], lang[0])

        current = self.colnect_locale.findData(settings['colnect_locale'])
        if current == -1:
            current = 9

        self.colnect_locale.setCurrentIndex(current)
        self.colnect_locale.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        fLayout.addRow(self.tr("Language"), self.colnect_locale)

        self.skip_currency = QCheckBox(self.tr("Skip currency symbol"), self)
        self.skip_currency.setChecked(settings['colnect_skip_currency'])
        fLayout.addRow(self.skip_currency)
        vLayout = QVBoxLayout()
        vLayout.addLayout(fLayout)
        colnectGroup = QGroupBox("Colnect", self)
        colnectGroup.setLayout(vLayout)

        clearCacheBtn = QPushButton(self.tr("Clear cache"), self)
        clearCacheBtn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        clearCacheBtn.clicked.connect(self.clearCache)
        hLayout = QHBoxLayout()
        hLayout.addWidget(clearCacheBtn, alignment=Qt.AlignRight)

        layout = QVBoxLayout()
        layout.addWidget(colnectGroup)
        layout.addLayout(hLayout)
        self.setLayout(layout)

    def clearCache(self):
        Cache.clear()
        infoMessageBox(
            "clear",
            self.tr("Clear cache"),
            self.tr("Import cache cleared"),
            parent=self
        )

    def save(self):
        settings = Settings()
        settings['colnect_locale'] = self.colnect_locale.currentData()
        settings['colnect_skip_currency'] = self.skip_currency.isChecked()
        settings.save()


@storeDlgSizeDecorator
class SettingsDialog(QDialog):

    def __init__(self, collection, parent=None):
        super().__init__(parent, Qt.WindowCloseButtonHint | Qt.WindowSystemMenuHint)
        self.setWindowTitle(self.tr("Settings"))
        self.tab = QTabWidget(self)
        self.tab.addTab(MainSettingsPage(collection, self), self.tr("Main"))
        if collection.isOpen():
            self.tab.addTab(CollectionSettingsPage(collection, self), self.tr("Collection"))
            self.tab.addTab(FieldsSettingsPage(collection, self), self.tr("Fields"))
            self.tab.addTab(ImportSettingsPage(self), self.tr("Import"))
        else:
            self.tab.setTabEnabled(self.tab.addTab(QWidget(), self.tr("Collection")), False)
            self.tab.setTabEnabled(self.tab.addTab(QWidget(), self.tr("Fields")), False)
            self.tab.setTabEnabled(self.tab.addTab(QWidget(), self.tr("Import")), False)

        buttonBox = QDialogButtonBox(Qt.Horizontal)
        buttonBox.addButton(QDialogButtonBox.Ok)
        buttonBox.addButton(QDialogButtonBox.Cancel)
        buttonBox.accepted.connect(self.save)
        buttonBox.rejected.connect(self.reject)

        layout = QVBoxLayout()
        layout.addWidget(self.tab)
        layout.addWidget(buttonBox)
        self.setLayout(layout)

    def save(self):
        for i in range(self.tab.count()):
            page = self.tab.widget(i)
            if page.isEnabled():
                page.save()

        self.accept()
